package com.yale.springbootrestful.controller;

import com.yale.springbootrestful.data.Greeting;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.concurrent.atomic.AtomicLong;
/**
 * Created by yale on 2017/8/17.
 *
 * 在spring，REST端点只是Spring MVC控制器。下面的Spring MVC的控制器处理一个GET请求/hello world和返回的Greeting资源：
 *
 */
@Controller
@RequestMapping("/rest")
public class RestController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @RequestMapping(method=RequestMethod.GET)
    public @ResponseBody Greeting sayHello(@RequestParam(value="name", required=false, defaultValue="Stranger") String name) {
        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }

}
